/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.gui.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.StatusDisplayer;
import pl.tom.social.analyzer.common.Results;
import pl.tom.social.analyzer.gui.panel.SiteSelectionPanel;
import pl.tom.social.analyzer.analysis.SimilaritiesSearch;
import pl.tom.social.analyzer.visualizer.AnalysisVisualizer;
import pl.tom.social.common.types.AnalysisType;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.SiteEntry;
import pl.tom.social.dal.entity.SiteEntryComment;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//pl.tom.social.analyzer.gui.component//SiteEntryTopComponent//EN",
autostore = false)
public final class SiteEntryTopComponent extends TopComponent {

	private static final Logger logger = Logger.getLogger(SiteEntryTopComponent.class);
	private static SiteEntryTopComponent instance;
	/** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
	private static final String PREFERRED_ID = "SiteEntryTopComponentTopComponent";
	private SiteEntry siteEntry;

	public SiteEntryTopComponent() {
		initComponents();
		setName(NbBundle.getMessage(SiteEntryTopComponent.class, "CTL_SiteEntryTopComponentTopComponent"));
		setToolTipText(NbBundle.getMessage(SiteEntryTopComponent.class,
				"HINT_SiteEntryTopComponentTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

	}

	public SiteEntryTopComponent(SiteEntry siteEntry) {
		this();
		if(siteEntry != null) {
			this.siteEntry = siteEntry;
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        siteEntryInfoPanel = new javax.swing.JPanel();
        siteEntryUrlLabel = new javax.swing.JLabel();
        siteEntryTitleLabel = new javax.swing.JLabel();
        siteEntryAddedLabel = new javax.swing.JLabel();
        siteEntryCommentsNrLabel = new javax.swing.JLabel();
        siteEntryOldestCommentLabel = new javax.swing.JLabel();
        siteEntryNewestCommentLabel = new javax.swing.JLabel();
        siteEntryUrInfo = new javax.swing.JLabel();
        siteEntryTitleInfo = new javax.swing.JLabel();
        siteEntryAddedInfo = new javax.swing.JLabel();
        siteEntryCommentsNrInfo = new javax.swing.JLabel();
        siteEntryOldestCommentInfo = new javax.swing.JLabel();
        siteEntryNewestCommentInfo = new javax.swing.JLabel();
        siteEntryCommentInfoPanel = new javax.swing.JPanel();
        siteEntryCommentUrlLabel = new javax.swing.JLabel();
        siteEntryCommentTitleLabel = new javax.swing.JLabel();
        siteEntryCommentAuthorLabel = new javax.swing.JLabel();
        siteEntryCommentAddedLabel = new javax.swing.JLabel();
        siteEntryCommentUrlInfo = new javax.swing.JLabel();
        siteEntryCommentTitleInfo = new javax.swing.JLabel();
        siteEntryCommentAuthorInfo = new javax.swing.JLabel();
        siteEntryCommentAddedInfo = new javax.swing.JLabel();
        analysisPanel = new javax.swing.JPanel();
        analysisFindButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        analysisResultSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        siteEntryBrowserScrollPane = new javax.swing.JScrollPane();
        siteEntryBrowserEditorPane = new javax.swing.JEditorPane();
        siteEntryCommentScrollPane = new javax.swing.JScrollPane();
        siteEntryCommentList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        showEntryButton = new javax.swing.JButton();
        deleteEntryButton = new javax.swing.JButton();

        setAutoscrolls(true);

        siteEntryInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryInfoPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryUrlLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryUrlLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryTitleLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryTitleLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryAddedLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryAddedLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentsNrLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentsNrLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryOldestCommentLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryOldestCommentLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryNewestCommentLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryNewestCommentLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryUrInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryUrInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryTitleInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryTitleInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryAddedInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryAddedInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentsNrInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentsNrInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryOldestCommentInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryOldestCommentInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryNewestCommentInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryNewestCommentInfo.text")); // NOI18N

        javax.swing.GroupLayout siteEntryInfoPanelLayout = new javax.swing.GroupLayout(siteEntryInfoPanel);
        siteEntryInfoPanel.setLayout(siteEntryInfoPanelLayout);
        siteEntryInfoPanelLayout.setHorizontalGroup(
            siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryUrlLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 519, Short.MAX_VALUE)
                        .addComponent(siteEntryUrInfo))
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryTitleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 512, Short.MAX_VALUE)
                        .addComponent(siteEntryTitleInfo))
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryAddedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 486, Short.MAX_VALUE)
                        .addComponent(siteEntryAddedInfo))
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryCommentsNrLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 457, Short.MAX_VALUE)
                        .addComponent(siteEntryCommentsNrInfo))
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryOldestCommentLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 455, Short.MAX_VALUE)
                        .addComponent(siteEntryOldestCommentInfo))
                    .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryNewestCommentLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 450, Short.MAX_VALUE)
                        .addComponent(siteEntryNewestCommentInfo)))
                .addContainerGap())
        );
        siteEntryInfoPanelLayout.setVerticalGroup(
            siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteEntryInfoPanelLayout.createSequentialGroup()
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryUrlLabel)
                    .addComponent(siteEntryUrInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryTitleLabel)
                    .addComponent(siteEntryTitleInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryAddedLabel)
                    .addComponent(siteEntryAddedInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryCommentsNrLabel)
                    .addComponent(siteEntryCommentsNrInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryOldestCommentLabel)
                    .addComponent(siteEntryOldestCommentInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryNewestCommentLabel)
                    .addComponent(siteEntryNewestCommentInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        siteEntryCommentInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentInfoPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentUrlLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentUrlLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentTitleLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentTitleLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentAuthorLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentAuthorLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentAddedLabel, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentAddedLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentUrlInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentUrlInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentTitleInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentTitleInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentAuthorInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentAuthorInfo.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(siteEntryCommentAddedInfo, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryCommentAddedInfo.text")); // NOI18N

        javax.swing.GroupLayout siteEntryCommentInfoPanelLayout = new javax.swing.GroupLayout(siteEntryCommentInfoPanel);
        siteEntryCommentInfoPanel.setLayout(siteEntryCommentInfoPanelLayout);
        siteEntryCommentInfoPanelLayout.setHorizontalGroup(
            siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryCommentUrlLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 519, Short.MAX_VALUE)
                        .addComponent(siteEntryCommentUrlInfo))
                    .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryCommentTitleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 512, Short.MAX_VALUE)
                        .addComponent(siteEntryCommentTitleInfo))
                    .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryCommentAuthorLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 499, Short.MAX_VALUE)
                        .addComponent(siteEntryCommentAuthorInfo))
                    .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                        .addComponent(siteEntryCommentAddedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 486, Short.MAX_VALUE)
                        .addComponent(siteEntryCommentAddedInfo)))
                .addContainerGap())
        );
        siteEntryCommentInfoPanelLayout.setVerticalGroup(
            siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(siteEntryCommentInfoPanelLayout.createSequentialGroup()
                .addGroup(siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryCommentUrlLabel)
                    .addComponent(siteEntryCommentUrlInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryCommentTitleLabel)
                    .addComponent(siteEntryCommentTitleInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryCommentAuthorLabel)
                    .addComponent(siteEntryCommentAuthorInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(siteEntryCommentInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteEntryCommentAddedLabel)
                    .addComponent(siteEntryCommentAddedInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        analysisPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.analysisPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(analysisFindButton, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.analysisFindButton.text")); // NOI18N
        analysisFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                analysisFindButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.jLabel1.text")); // NOI18N

        analysisResultSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(1), null, Integer.valueOf(1)));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout analysisPanelLayout = new javax.swing.GroupLayout(analysisPanel);
        analysisPanel.setLayout(analysisPanelLayout);
        analysisPanelLayout.setHorizontalGroup(
            analysisPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(analysisPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(analysisResultSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(163, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, analysisPanelLayout.createSequentialGroup()
                .addContainerGap(212, Short.MAX_VALUE)
                .addComponent(analysisFindButton)
                .addContainerGap())
        );
        analysisPanelLayout.setVerticalGroup(
            analysisPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(analysisPanelLayout.createSequentialGroup()
                .addGroup(analysisPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(analysisResultSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                .addComponent(analysisFindButton)
                .addContainerGap())
        );

        siteEntryBrowserEditorPane.setContentType(org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.siteEntryBrowserEditorPane.contentType")); // NOI18N
        siteEntryBrowserEditorPane.setEditable(false);
        siteEntryBrowserEditorPane.setMaximumSize(new java.awt.Dimension(800, 2147483647));
        siteEntryBrowserScrollPane.setViewportView(siteEntryBrowserEditorPane);

        siteEntryCommentScrollPane.setViewportView(siteEntryCommentList);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.jPanel1.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(showEntryButton, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.showEntryButton.text")); // NOI18N
        showEntryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showEntryButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteEntryButton, org.openide.util.NbBundle.getMessage(SiteEntryTopComponent.class, "SiteEntryTopComponent.deleteEntryButton.text")); // NOI18N
        deleteEntryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteEntryButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(165, Short.MAX_VALUE)
                .addComponent(deleteEntryButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(showEntryButton)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(showEntryButton)
                    .addComponent(deleteEntryButton))
                .addContainerGap(10, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(siteEntryInfoPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteEntryCommentInfoPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteEntryBrowserScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(analysisPanel, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteEntryCommentScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(siteEntryBrowserScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                    .addComponent(siteEntryCommentScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(siteEntryInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(siteEntryCommentInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(analysisPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void analysisFindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analysisFindButtonActionPerformed
		final SiteSelectionPanel panel = new SiteSelectionPanel();
		DialogDescriptor dialog = new DialogDescriptor(panel, "Select sites", true, new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				int limit = (Integer) analysisResultSpinner.getValue();
				Map<String, Float> result = SimilaritiesSearch.getSimilar(siteEntry, panel.getSelected(), limit);
				TableModel model = AnalysisVisualizer.similarEntriesToJTable(result);
				Results.show(model, AnalysisType.SimilaritiesSearch);
			}
		});
		DialogDisplayer.getDefault().notify(dialog);
	}//GEN-LAST:event_analysisFindButtonActionPerformed

	private void showEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showEntryButtonActionPerformed
		siteEntryBrowserEditorPane.setText(siteEntry.getBody());
	}//GEN-LAST:event_showEntryButtonActionPerformed

	private void deleteEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteEntryButtonActionPerformed
		this.close();

		SocialDatabaseFacade db = new SocialDatabaseFacade();
		SiteEntry entry;
		try {
			entry = db.getSiteEntry(siteEntry.getId());
			db.deleteEntity(entry);
			StatusDisplayer.getDefault().setStatusText("Entry deleted successfully");
		} catch(Exception ex) {
			logger.log(Level.ERROR, ex);
			StatusDisplayer.getDefault().setStatusText("Error while deleting entry. Check the logs!");
		}
	}//GEN-LAST:event_deleteEntryButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton analysisFindButton;
    private javax.swing.JPanel analysisPanel;
    private javax.swing.JSpinner analysisResultSpinner;
    private javax.swing.JButton deleteEntryButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton showEntryButton;
    private javax.swing.JLabel siteEntryAddedInfo;
    private javax.swing.JLabel siteEntryAddedLabel;
    private javax.swing.JEditorPane siteEntryBrowserEditorPane;
    private javax.swing.JScrollPane siteEntryBrowserScrollPane;
    private javax.swing.JLabel siteEntryCommentAddedInfo;
    private javax.swing.JLabel siteEntryCommentAddedLabel;
    private javax.swing.JLabel siteEntryCommentAuthorInfo;
    private javax.swing.JLabel siteEntryCommentAuthorLabel;
    private javax.swing.JPanel siteEntryCommentInfoPanel;
    private javax.swing.JList siteEntryCommentList;
    private javax.swing.JScrollPane siteEntryCommentScrollPane;
    private javax.swing.JLabel siteEntryCommentTitleInfo;
    private javax.swing.JLabel siteEntryCommentTitleLabel;
    private javax.swing.JLabel siteEntryCommentUrlInfo;
    private javax.swing.JLabel siteEntryCommentUrlLabel;
    private javax.swing.JLabel siteEntryCommentsNrInfo;
    private javax.swing.JLabel siteEntryCommentsNrLabel;
    private javax.swing.JPanel siteEntryInfoPanel;
    private javax.swing.JLabel siteEntryNewestCommentInfo;
    private javax.swing.JLabel siteEntryNewestCommentLabel;
    private javax.swing.JLabel siteEntryOldestCommentInfo;
    private javax.swing.JLabel siteEntryOldestCommentLabel;
    private javax.swing.JLabel siteEntryTitleInfo;
    private javax.swing.JLabel siteEntryTitleLabel;
    private javax.swing.JLabel siteEntryUrInfo;
    private javax.swing.JLabel siteEntryUrlLabel;
    // End of variables declaration//GEN-END:variables

	/**
	 * Gets default instance. Do not use directly: reserved for *.settings files only,
	 * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
	 * To obtain the singleton instance, use {@link #findInstance}.
	 */
	public static synchronized SiteEntryTopComponent getDefault() {
		if(instance == null) {
			instance = new SiteEntryTopComponent();
		}
		return instance;
	}

	/**
	 * Obtain the SiteEntryTopComponentTopComponent instance. Never call {@link #getDefault} directly!
	 */
	public static synchronized SiteEntryTopComponent findInstance() {
		TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
		if(win == null) {
			Logger.getLogger(SiteEntryTopComponent.class.getName()).warn(
					"Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
			return getDefault();
		}
		if(win instanceof SiteEntryTopComponent) {
			return (SiteEntryTopComponent) win;
		}
		Logger.getLogger(SiteEntryTopComponent.class.getName()).warn(
				"There seem to be multiple components with the '" + PREFERRED_ID +
				"' ID. That is a potential source of errors and unexpected behavior.");
		return getDefault();
	}

	@Override
	public int getPersistenceType() {
		return TopComponent.PERSISTENCE_NEVER;
	}

	@Override
	public void componentOpened() {
		if(siteEntry != null) {
			setSiteEntryInfo(siteEntry);
			setComments();
		} else {
			NotifyDescriptor dialog = new NotifyDescriptor.Message(
					"No site entry defined for this window. Site entry is not downloaded or not set properly on window opening.",
					NotifyDescriptor.WARNING_MESSAGE);
			DialogDisplayer.getDefault().notify(dialog);
			this.close();
		}
	}

	@Override
	public void componentClosed() {
	}

	void writeProperties(java.util.Properties p) {
		// better to version settings since initial version as advocated at
		// http://wiki.apidesign.org/wiki/PropertyFiles
		p.setProperty("version", "1.0");
	}

	Object readProperties(java.util.Properties p) {
		SiteEntryTopComponent singleton = SiteEntryTopComponent.getDefault();
		singleton.readPropertiesImpl(p);
		return singleton;
	}

	private void readPropertiesImpl(java.util.Properties p) {
		String version = p.getProperty("version");
	}

	@Override
	protected String preferredID() {
		return PREFERRED_ID;
	}

	private void setComments() {
		SocialDatabaseFacade db = new SocialDatabaseFacade();
		List<SiteEntryComment> comments = db.getSiteEntryComments(siteEntry);
		if(comments != null && !comments.isEmpty()) {
			this.siteEntryCommentsNrInfo.setText(String.valueOf(comments.size()));
			this.siteEntryNewestCommentInfo.setText(comments.get(comments.size() - 1).getAdded().toString());
			this.siteEntryOldestCommentInfo.setText(comments.get(0).getAdded().toString());
			this.siteEntryCommentList.setListData(comments.toArray());
			this.siteEntryCommentList.repaint();
		}

		siteEntryCommentList.addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent e) {
				Object o = ((JList) e.getSource()).getSelectedValue();
				if(o instanceof SiteEntryComment) {
					SiteEntryComment siteEntryComment = (SiteEntryComment) o;
					siteEntryBrowserEditorPane.setText(siteEntryComment.getBody());
					siteEntryCommentUrlInfo.setText(siteEntryComment.getUrl());
					if(siteEntryComment.getTitle() != null) {
						siteEntryCommentTitleInfo.setText(siteEntryComment.getTitle());
					}
					if(siteEntryComment.getAuthor() != null) {
						siteEntryCommentAuthorInfo.setText(siteEntryComment.getAuthor());
					}
					siteEntryCommentAddedInfo.setText(siteEntryComment.getAdded().toString());
				}
			}
		});
	}

	private void setSiteEntryInfo(SiteEntry siteEntry) {
		siteEntryUrInfo.setText(siteEntry.getUrl());
		if(siteEntry.getTitle() != null) {
			siteEntryTitleInfo.setText(siteEntry.getTitle());
		}
		siteEntryAddedInfo.setText(siteEntry.getAdded().toString());
		siteEntryBrowserEditorPane.setText(siteEntry.getBody());
	}
}
